package com.example.gulimall.product.controller;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.example.gulimall.product.entity.ProductAttrValueEntity;
import com.example.gulimall.product.service.ProductAttrValueService;
import com.example.gulimall.product.vo.AttrRespVo;
import com.example.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gulimall.product.entity.AttrEntity;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.common.utils.PageUtils;
import com.example.gulimall.common.utils.R;



/**
 * 商品属性
 *
 * @author xie
 * @email 2949459108@gmail.com
 * @date 2024-01-18 21:07:08
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catId}")
    public R baseList(@RequestParam Map<String, Object> params,@PathVariable(value = "type")String attrType,@PathVariable(value = "catId")Long catelogId){
        PageUtils page = attrService.queryBaseAttrPage(params,attrType,catelogId);
        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
        AttrRespVo attrInfo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attr){
		attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }
    /**
     * 获取spu规格
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseListForSpu(@PathVariable(value = "spuId") Long spuId){
       List<ProductAttrValueEntity> productAttrValueEntityLinkedList=productAttrValueService.baseListForSpu(spuId);

        return R.ok().put("data",productAttrValueEntityLinkedList);
    }
    /**
     * 修改spu规格
     */
    @GetMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable(value = "spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> list){
      productAttrValueService.updateSpuAttr(spuId,list);

        return R.ok();
    }
}
